package com.ruoyi.gis.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.gis.vo.CollectAddFolderDto;
import com.ruoyi.gis.vo.CollectAddImageDto;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gis.domain.DatasetRasterCollected;
import com.ruoyi.gis.service.IDatasetRasterCollectedService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户收藏的数据集Controller
 *
 * @author rww
 * @date 2024-08-30
 */
@RestController
@RequestMapping("/earthai/userCollected")
public class DatasetRasterCollectedController extends BaseController
{
    @Autowired
    private IDatasetRasterCollectedService datasetRasterCollectedService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询用户收藏的数据集列表
     */
//    @PreAuthorize("@ss.hasPermi('earthai:userCollected:list')")
    @GetMapping("/list")
    public TableDataInfo list(HttpServletRequest request,
            DatasetRasterCollected datasetRasterCollected)
    {
        datasetRasterCollected.setUserId(tokenService.getLoginUser(request).getUserId());
        startPage();
        List<DatasetRasterCollected> list = datasetRasterCollectedService.
                selectDatasetRasterCollectedList(datasetRasterCollected);
        return getDataTable(list);
    }
    @GetMapping("/list/all")
    public AjaxResult listAll(HttpServletRequest request,
                              DatasetRasterCollected datasetRasterCollected)
    {
        datasetRasterCollected.setUserId(tokenService.getLoginUser(request).getUserId());
        List<DatasetRasterCollected> list = datasetRasterCollectedService.
                selectDatasetRasterCollectedList(datasetRasterCollected);
        return AjaxResult.success(list);
    }
    @GetMapping("/list/all/folder")
    public AjaxResult listAllFolder(HttpServletRequest request,
                              DatasetRasterCollected datasetRasterCollected)
    {
        datasetRasterCollected.setUserId(tokenService.getLoginUser(request).getUserId());
        datasetRasterCollected.setType(DatasetRasterCollected.type_folder);
        List<DatasetRasterCollected> list = datasetRasterCollectedService.
                selectDatasetRasterCollectedList(datasetRasterCollected);
        return AjaxResult.success(list);
    }

    /**
     * 导出用户收藏的数据集列表
     */
//    @PreAuthorize("@ss.hasPermi('earthai:userCollected:export')")
    @Log(title = "用户收藏的数据集", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DatasetRasterCollected datasetRasterCollected)
    {
        List<DatasetRasterCollected> list = datasetRasterCollectedService.selectDatasetRasterCollectedList(datasetRasterCollected);
        ExcelUtil<DatasetRasterCollected> util = new ExcelUtil<DatasetRasterCollected>(DatasetRasterCollected.class);
        util.exportExcel(response, list, "用户收藏的数据集数据");
    }

    /**
     * 获取用户收藏的数据集详细信息
     */
//    @PreAuthorize("@ss.hasPermi('earthai:userCollected:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(datasetRasterCollectedService.selectDatasetRasterCollectedById(id));
    }

    /**
     * 新增用户收藏的数据集
     */
//    @PreAuthorize("@ss.hasPermi('earthai:userCollected:add')")
    @Log(title = "用户收藏的数据集", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DatasetRasterCollected datasetRasterCollected)
    {
        return toAjax(datasetRasterCollectedService.insertDatasetRasterCollected(datasetRasterCollected));
    }
    @PostMapping("addFolder")
    public AjaxResult addFolder(HttpServletRequest request,
                                @RequestBody CollectAddFolderDto dto)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId = loginUser.getUserId();
        DatasetRasterCollected folder = datasetRasterCollectedService.addFolder(
                userId,dto.getPid(),dto.getName());
        if(folder!=null){
            return AjaxResult.success(folder);
        }else {
            return AjaxResult.error("添加目录失败");
        }
    }
    @PostMapping("addImages")
    public AjaxResult addImages(HttpServletRequest request,
                                @RequestBody CollectAddImageDto dto)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if(dto.getDataIds()==null||dto.getDataIds().size()==0){
            return AjaxResult.error("dataIds参数错误");
        }
        datasetRasterCollectedService.addCollectImages(loginUser,dto);
        return AjaxResult.success();
    }




    /**
     * 修改用户收藏的数据集
     */
//    @PreAuthorize("@ss.hasPermi('earthai:userCollected:edit')")
    @Log(title = "用户收藏的数据集", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DatasetRasterCollected datasetRasterCollected)
    {
        return toAjax(datasetRasterCollectedService.updateDatasetRasterCollected(datasetRasterCollected));
    }

    /**
     * 删除用户收藏的数据集
     */
//    @PreAuthorize("@ss.hasPermi('earthai:userCollected:remove')")
    @Log(title = "用户收藏的数据集", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(datasetRasterCollectedService.deleteDatasetRasterCollectedByIds(ids));
    }
}
